﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using TomNet.App.Core.Contracts;
using TomNet.App.Model.DB;
using TomNet.App.Model.DTO;
using TomNet.AspNetCore.Data;
using TomNet.SqlSugarCore.Entity;
using TomNet.Security.Claims;
using System.Security.Claims;

namespace TomNet.App.Web.Controllers
{
    [Authorize]
    [Description("商品管理")]
    public class ProductController : Controller
    {
        private readonly IProductContract _productContract;        
        private readonly IUnitOfWork _unitOfWork;
        public ProductController(IProductContract productContract,            
            IUnitOfWork unitOfWork)
        {
            _productContract = productContract;
            _unitOfWork = unitOfWork;
        }
        [Description("商品列表")]
        [HttpGet]
        public IActionResult ProductList()
        {
            return View();
        }
        [Description("商品列表")]
        [HttpPost]
        public IActionResult ProductList(int page = 1, int limit = 20)
        {
            var entry = _productContract.Entities.AsQueryable();
            //entry.WhereIF(!string.IsNullOrEmpty(name), m => m..Contains(name));
            entry.OrderBy(m => m.Id, OrderByType.Desc);
            int total = 0;
            var list = entry.ToPageList(page, limit, ref total);
            var result = new LayuiPageResult<Product>
            {
                Code = 0,
                Msg = "Success",
                Count = total,
                Data = list.ToArray()
            };
            return Json(result);
        }
        [Description("商品添加")]
        [HttpGet]
        public IActionResult ProductInsert()
        {
            return View();
        }
        [Description("商品添加")]
        [HttpPost]
        public IActionResult ProductInsert(Product product)
        {
            AjaxResult result = new AjaxResult { Type = Data.AjaxResultType.Error, Content = "未知异常", Data = null };
            product.HtId = 0;
            product.Kwh = 0;
            product.Image = "";
            var count = _productContract.Entities.AsQueryable().Where(m => m.Name == product.Name).Count();
            if (count > 0)
            {
                result.Content = "产品名称已经存在";
            }
            else
            {
                result.Content = "操作成功";
                result.Type = Data.AjaxResultType.Success;
                _productContract.Insert(product);
            }
                        
            return Json(result);
        }
        [Description("商品修改")]
        [HttpGet]
        public IActionResult ProductEdit(int id)
        {
            var goods = _productContract.Entities.AsQueryable().Where(m => m.Id == id).First();
            return View(goods);
        }
        [Description("商品修改")]
        [HttpPost]
        public IActionResult ProductEdit(Product product)
        {
            AjaxResult result = new AjaxResult { Type = Data.AjaxResultType.Error, Content = "未知异常", Data = null };
            product.HtId = 0;
            product.Kwh = 0;
            product.Image = "";
            var count = _productContract.Entities.AsQueryable().Where(m => m.Name == product.Name).Count();
            if (count > 0)
            {
                result.Content = "产品名称已经存在";
            }
            else
            {
                result.Content = "操作成功";
                result.Type = Data.AjaxResultType.Success;
                _productContract.Update(product);
            }
            return Json(result);
        }
        [Description("商品删除")]
        [HttpPost]
        public IActionResult ProductDelete(int id)
        {
            AjaxResult result = new AjaxResult { Type = Data.AjaxResultType.Error, Content = "未知异常", Data = null };
            int count = _productContract.Delete(id);
            if (count > 0)
            {
                result.Content = "操作成功";
                result.Type = Data.AjaxResultType.Success;
            }
            else
            {
                result.Content = "操作失败";
                result.Type = Data.AjaxResultType.Error;
            }
            return Json(result);
        }
    }
}